import React from 'react';
import axios from 'axios';
import { apiPrefix } from '../../../shared/api';
import Td from './TableTr';
import './index.less';

export default class JobSubsidy extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      newJobSubsidy: {},
      jobSubsidy: [],
      show: false,
    };
  }
  componentDidMount() {
    const api = `${apiPrefix}wdy_wage/api/v1//wage/job_allowances`;
    axios.get(api)
      .then((response) => {
        this.setState({ jobSubsidy: response.data.data });
        console.log(this.state.jobSubsidy);
      });
  }
  handleOnChange=(value, key) => {
    const { newJobSubsidy } = this.state;
    newJobSubsidy[key] = value;
    this.setState({ newJobSubsidy: { ...this.state.newJobSubsidy } });
    console.log(newJobSubsidy);
  }
  addNewJobSubsidy=() => {
    const { newJobSubsidy } = this.state;
    const api = `${apiPrefix}wdy_wage/api/v1//wage/job_allowance`;
    axios.post(api, newJobSubsidy)
      .then((response) => {
        console.log(response);
      });
  }
  showAddInput=() => {
    this.setState({ show: true });
  }
  cancelAdd=() => {
    this.setState({ show: false, newJobSubsidy: {} });
  }
  sureEdit=(val) => {
    console.log(val);
    const api = `${apiPrefix}wdy_wage/api/v1//wage/job_allowance?id=${val.id}`;
    axios.put(api, val)
      .then((response) => {
        console.log(response.status === 200);
        if (response.status === 200) {
          this.componentDidMount();
        }
      });
  }
  render() {
    const { jobSubsidy, show, newJobSubsidy } = this.state;
    return (
      <div className="content">
        <div>
          <h3>岗位津贴</h3>
          <table>
            <thead>
              <tr>
                <th>序号</th>
                <th>岗位津贴名称</th>
                <th>岗位津贴金额</th>
                <th>备注/描述</th>
                <th>
                  <button onClick={this.showAddInput} className="addButton">添加</button>
                </th>
              </tr>
            </thead>
            <tbody>
              <tr className={show ? 'add-contract-type' : 'hide'}>
                <td />
                <td>
                  <input
                    value={newJobSubsidy.name || ''}
                    type="text"
                    onChange={e => this.handleOnChange(e.target.value, 'name')}
                  />
                </td>
                <td>
                  <input
                    value={newJobSubsidy.amount || ''}
                    type="number"
                    onChange={e => this.handleOnChange(e.target.value, 'amount')}
                  />
                </td>
                <td>
                  <input
                    value={newJobSubsidy.descr || ''}
                    type="text"
                    onChange={e => this.handleOnChange(e.target.value, 'descr')}
                  />
                </td>
                <td>
                  <button className="green-button" onClick={this.addNewJobSubsidy}>保存</button>
                  <button className="clearButton" onClick={this.cancelAdd}>取消</button>
                </td>
              </tr>
              {
                jobSubsidy.map((value, index) => (
                  <tr key={index}>
                    <Td
                      sureEdit={val => this.sureEdit(val)}
                      value={value}
                      index={index}
                    />
                  </tr>
                ))
              }
            </tbody>
          </table>
        </div>
      </div>
    );
  }
}
